<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
    const RE_MOBILE='/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i';
    private $_isMobile; //Atribut per a guardar si el client accedeix des d'un dispositiu mòbil
    
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
    
    /* Funció per determinar si s'accedeix des d'un dispositiu mòbil
     * per a utilitzar-la es pot cridar des dels controladors fills: 
     * 
     * public function init(){
            if ($this->getIsMobile())
                //$this->layout='//layouts/mobile'
     *          Yii::app()->theme= 'mobile';
     *      parent::init();
        }
     * 
     */
    public function getIsMobile(){
        if ($this->_isMobile===null){
            $this->_isMobile=isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']) ||
                preg_match(self::RE_MOBILE, $_SERVER['HTTP_USER_AGENT']);
            Yii::app()->user->setState('dispositiu', ($this->_isMobile ? 'PDA' : 'PC'));
        }
        return $this->_isMobile;    
    }
}